import requests
from bs4 import BeautifulSoup
def gethtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = "utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""
def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    div_ls=soup.find_all("div",{"class":"news_list"})
    all_ls=[]
    for li in div_ls[0].find_all("li"):
        print(li)
        ls=[]
        div_data=li.find("div",{"class":"date_1"})
        ls.append(div_data.string)
        a_ls=li.find("a")
        ls.append(a_ls.attrs["title"])
        ls.append(a_ls.attrs['href'])
        all_ls.append(ls)
    return all_ls
def writehtmldata(ls):
    with open(r"山西工程职业技术学院.csv","w") as f:
        for line in ls:
            f.write(",".join(line)+"\n")
def main():
    url="http://www.sxit.edu.cn/"
    html=gethtml(url)
    ls=gethtmldata(html)
    writehtmldata(ls)
if __name__ == '__main__':
    main()